#include <stdio.h>
 
int main()
{
  int array[100];
  int n, i, t, h, x, y, swap;
  int count = 0;

  scanf("%d", &t);
 
  for (h = 0; h < t; h++){
		scanf("%d", &n);
  for (i = 0; i < n; i++)
		scanf("%d", &array[i]);
  for (x = 0 ; x < ( n - 1 ); x++)
  {
    for (y = 0 ; y < n - x - 1; y++)
    {
      if (array[y] > array[y+1])
      {
        swap       = array[y];
        array[y]   = array[y+1];
        array[y+1] = swap;
		count++; 
      }
    }
  }
	 /*for ( i = 0 ; i < n ; i++ )   // for printing sorted array
		printf("%d ", array[i]); 
	 printf("\n");*/
     printf("Optimal train swapping takes %d swaps.\n", count);
	 count = 0;
  }
  return 0;
}

